package com.linhai.scm.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import com.linhai.scm.entity.base.User;
import com.linhai.scm.service.IUserService;

@WebServlet("/Client.h")
public class ClientServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private IUserService service;
    
    public ClientServlet() {
        super();
    }

	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		if(null==service){
			service=(IUserService)org.springframework.web.context.support.WebApplicationContextUtils
                .getRequiredWebApplicationContext(getServletContext()).getBean(
                        "userService");
		}
        System.out.println("ClientServlet初始化!");
        
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		java.io.PrintWriter out=response.getWriter();
		String msg="";
		String strJson="";
		if(null==service){
			msg="失败，系统初始化错误！";
			strJson="{\"ok\":false,\"msg\":\"" + msg + "\"}";
			out.write(strJson);
			return;
		}
		String uName=request.getParameter("uName");
		String uPwd=request.getParameter("uPwd");
		User entity=new User();
		entity.setUserName(uName);
		entity.setUserPwd(uPwd);
		if(service.auth(entity)){
			msg="成功";
			strJson="{\"ok\":true,\"msg\":\"" + msg + "\"}";
		}else{
			msg="失败，用户名或密码不正确！";
			strJson="{\"ok\":false,\"msg\":\"" + msg + "\"}";
		}
		out.write(strJson);
	}

}
